﻿import numpy as np

# 1.数组形状
print("数组形状：")
print(np.array([[1, 2], [3, 4], [5, 6]]))
print(np.array([[1, 2], [3, 4], [5, 6]]).shape) # (3, 2) 三行两列
print("reshape：")
print(np.array([[1, 2], [3, 4], [5, 6]]).reshape(1, 6)) # 一行六列
print("resize：")
print(np.resize(np.array([[1, 2], [3, 4], [5, 6]]), (2, 2))) # 两行两列

# 2.转置
print("转置：")
print(np.array([[1, 2], [3, 4], [5, 6]]).T)

# 3.数组的复制
src_arr = np.arange(4)
cpy_arr = src_arr.copy()
cpy_arr[1] = 7
print("数组的复制：")
print(src_arr, cpy_arr)

# 4.数组元素类型转换
float_arr = np.arange(4, dtype=float)
int_arr = float_arr.astype(np.int64)
print("数组元素类型转换：")
print(float_arr, float_arr.dtype)
print(int_arr, int_arr.dtype)

# 5.数组的连接
stack_arr1 = np.arange(1, 5)
stack_arr2 = np.arange(5, 9)
stack_arr_h = np.hstack((stack_arr1, stack_arr2))
print("数组的横向连接：")
print(stack_arr_h)
stack_arr_v = np.vstack((stack_arr1, stack_arr2))
print("数组的竖向连接：")
print(stack_arr_v)

# 6.数组的分割
split_arr = np.arange(16).reshape(4, 4)
print("数组的分割：\n原始数组")
print(split_arr)
print("数组的横向分割：")
print(np.hsplit(split_arr, 2))
print("数组的竖向分割：")
print(np.vsplit(split_arr, 4))

# 7.数组的简单运算
print("数组的简单运算：\n原始数组")
arr = np.arange(6).reshape(2, 3)
print(arr)
print("加法：")
print(arr + 1)
print("乘法：")
print(arr * 2)
print("除法：")
print(1 / (arr + 1))
print("幂运算：")
print(arr ** 0.5)

print("均值：")
print(arr.mean())
print("最大值：")
print(arr.max())
print("最小值：")
print(arr.min())
print("标准差：")
print(arr.std())
print("方差：")
print(arr.var())
print("求和：")
print(arr.sum())
print("按列求和：")
print(np.sum(arr, axis=0))
print("按行求和：")
print(np.sum(arr, axis=1))

print("排序：")
print(np.sort(np.array([3, 5, 2, 1, 4])))